<?php 
ob_start(); session_start();
require_once('config/setting.php');
if (!$_SESSION['userid'])
{
	header('location: login.php');
	
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>The Century - Admin Panel</title>
	<style>
        .topmenu{
            font-family:arial;
			font-weight:bold;
            font-size:16px;
            color:#FFFFFF;
            text-decoration:none;
        }
        .topmenu:hover{
            font-family:arial;
			font-weight:bold;
            font-size:16px;
            color:#FFFFFF;
            text-decoration:underline;
        }
    </style>
</head>
<body>
<?php 
	if($_POST['submitans']){
		$qid = $_POST['qid'];
		$answer = $_POST['answer'];
		$datein = time();
		mysql_query("insert into tbl_answer (id,qid,answer,dateposted) values ('','$qid','$answer','$datein')") or die (mysql_error());
		header('Location: question.php');		
	}
?>
<table width="100%">
<div style="padding:10px; font-size:20px; font-family:verdana;"><span>THE CENTURY <span style="font-size:14px">Admin Panel</span></span></div>

<tr>
	<td style="width:100%; height:auto; ">
	  <table  style="width:100%; height:100%" border="0">
		<tr>
			<td style="background:#FF0000; height:71px;">
			   <div align="center">	
				   <table style="width:960px;" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td style="height:42px; padding-top:15px"  >
								<table align="center" cellpadding="0" cellspacing="0" border="0">
									<tr>
										<td style="font-weight:bold; color:#FFFFFF">
                                        	<a href="view-customers.php" class="topmenu">View Customers</a> &nbsp;|&nbsp; 
											<a href="reports.php" class="topmenu">Reports</a> &nbsp;|&nbsp; 
                                            <a href="reservation.php" class="topmenu">Reservation</a> &nbsp;|&nbsp; 
                                            <a href="slots.php" class="topmenu">Slots</a> &nbsp;|&nbsp;  
                                            <a href="question.php" class="topmenu">Question</a> &nbsp;|&nbsp;  
                                            <a href="menus.php" class="topmenu">Menus</a> &nbsp;|&nbsp;  
                                            <a href="logout.php" class="topmenu">Logout</a> 
                                        </td>
									</tr>
								</table>							
                             </td>
						</tr>
					</table>			
				</div>			
              </td>
		</tr>	
	</table></td>
</tr>
<!--content-->
<tr>
	<td style="width:800px; font-family:arial; font-size:14px;  height:500px;" valign="top" align="center">
    	<? if($_GET['action']=='post answer'){ 
			$qid = $_GET['qid'];
			$qresult = mysql_query("select * from tbl_question where id ='$qid' limit 1 ") or die (mysql_error());
			if ($qrow = mysql_fetch_array($qresult)){
				$question = $qrow['question'];
				$detail = $qrow['detail'];
			}
		?>
    	<div style="width:550px; " align="left">
        
        	<form name="aform" method="post">
            	<b>Question:</b> <?=$question;?><br><br>
                <b>Details:</b> <?=$detail;?><br><br>
                Answer:<br>
                <input name="qid" type="hidden" value="<?=$qid;?>">
                <textarea name="answer" style="width:500px; height:200px ;margin-bottom:10px; "></textarea>
                <input name="submitans" type="submit" value="Post Answer" style="width:150px; height:40px" onClick="return validateField();" >
            </form>
        <? } ?>    
        </div>	
        <? if($_GET['action']!='post answer'){ ?>
    	 <table  style="width:800px; border:solid 1px  #999999; font-family:arial; font-size:12px" >
        	<tr>
            	<td width="45%" height="30px" align="center" bgcolor="#999999" style="color:#FFF" ><b>Questions</b></td>
                <td width="40%" height="30px" align="center" bgcolor="#999999" style="color:#FFF" ><b>Answers</b></td>
                <td width="15%" height="30px" align="center" bgcolor="#999999" style="color:#FFF" >Action</td>
            </tr>
			<?php
					$result = mysql_query("select * from tbl_question order by id DESC ") or die (mysql_error());
					while ($row = mysql_fetch_array($result)){
						$id = $row['id']; 
						$mid = $row['mid']; 
						$question = $row['question']; 
						$answers = '';
						$aresult = mysql_query("SELECT * FROM tbl_answer where qid='$id' ") or die (mysql_error());
						while ($arow = mysql_fetch_array($aresult)){
							$answers .= '<p style="color:#000000"> '.$arow['answer'].'</p>';
						}						
						if(empty($answers)){
							echo '
								<tr >
									<td align="left" height="26px" style="border-bottom:dotted #666666 1px; padding-left:10px"><b>'.$question.'</b></td>
									<td align="left" height="26px" style="border-bottom:dotted #666666 1px;">'.$answers.' </td>
									
									<td align="center" height="24px" style="border-bottom:dotted #666666 1px;"><a href="question.php?action=post answer&qid='.$id.'" >Post Answer</a></td>
								</tr>';
						}else{
							echo '
								<tr >
									<td align="left" height="26px" style="border-bottom:dotted #666666 1px; padding-left:10px"><b>'.$question.'</b></td>
									<td align="left" height="26px" style="border-bottom:dotted #666666 1px;">'.$answers.' </td>
									
									<td align="center" height="24px" style="border-bottom:dotted #666666 1px;">Already Answered</a></td>
								</tr>';
							
						}
					}                
                ?>                        
        </table>
        <? } ?>
     </td>
</tr>
<!--footer-->
<tr>
	<td style="width:100%;"><table style="width:100%; height:100%">
		<tr>
			<td style="width:50%;"><img alt="" src="../images/spacer.gif" width="1" height="1"></td>
			<td style="width:766px;"><table style="width:766px;">
				<tr><td class="end"><table>
					
				</table></td></tr>
				<tr>
				  <td class="end">&nbsp;</td>
			  </tr>
			</table></td>
			<td style="width:50%;"><img alt="" src="../images/spacer.gif" width="1" height="1"></td>
		</tr>
	</table></td>
</tr>
</table>
<script>
	function validateField(){
		var emptyfield = '';
		
		if(document.aform.answer.value.trim()==''){
			emptyfield += ' Your Answer \n';
		}
		if(emptyfield==''){
			return true;				
		}else{
			alert('Please complete required fields: \n'+emptyfield);
			return false;
		}
	}

</script>    
</body>
</html>
